﻿using Blog.IResponse;
using BlogModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Response
{
    /*
     使用Mapper一对一,一对多，多对多 模式需要设置2-3个参数， Mapper( 1需要填充的子对象 ,2主表关联字段，3从表关联字段（默认为从表主键）)
     */
    public class BlogBlogNewsResponse:BaseResponse<BlogNews>, IBlogBlogNewsResponse
    {
        //重写父类中的方法，进行导航查询
        public override async Task<IEnumerable<BlogNews>> QueryAsync()
        {
            return await base.Context.Queryable<BlogNews>()
                    .Mapper(blognews => blognews.TypeInfo, blognews => blognews.TypeId)
                    .Mapper(blo => blo.Author, blo => blo.AuthorId)
                    .ToListAsync();    
        }

        public override async Task<IEnumerable<BlogNews>> QueryPageListAsync(Expression<Func<BlogNews, bool>> expression, PageModel pageModel)
        {
            pageModel.PageCount = base.Count(expression);
            return await base.Context.Queryable<BlogNews>()
                .Mapper(blo => blo.Author, blo => blo.AuthorId)
                .Mapper(blo => blo.TypeInfo, blo => blo.TypeId)
                .Where(expression)
                .Skip(pageModel.PageIndex)
                .Take(pageModel.PageSize)
                .ToListAsync();
        }

    }
}
